package org.sdnstproject.interlayer.OF13Protocal;

import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuTableMod {
	private static final Logger log = LoggerFactory.getLogger(FuTableMod.class);
    public byte version = (byte)4;
    public byte type = (byte)17;
    public short length = (short)16;//fixed value property length = 16
    public long xid = 0x0;
    public byte tableId;//TableId TODO
    public long config;//TODO
    
    public FuTableMod() {
    	
    }
    
    public FuTableMod setXid(long xid) {
		this.xid = xid;
		return this;
	}
    
    public FuTableMod setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "tableId":
			this.tableId = Byte.parseByte(value);
			break;
		case "config":
			this.config = Long.parseLong(value);
			break;
		}
		return this;
    }
    
    public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
        // fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 17
        bb.writeByte(type);
        // fixed value property length = 16
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        bb.writeByte(tableId);
        // pad: 3 bytes
        bb.writeZero(3);
        bb.writeInt(U32.t(config));
        return bb;
    }
    
    
    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFFTableModVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(", ");
        b.append("tableId=").append(tableId);
        b.append(", ");
        b.append("config=").append(config);
        b.append(")");
        return b.toString();
    }
}
